package com.sumainfor.pc.gmio.service.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.HtmlUtil;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.gmio.dao.SeDoubtMapper;
import com.sumainfor.pc.gmio.dao.UtilsMapper;
import com.sumainfor.pc.gmio.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.gmio.service.SeDoubtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.awt.*;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 服务中心经理房源定价
 */
@Service
public class SeDoubtServiceImpl implements SeDoubtService, Serializable {

    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(SeDoubtService.class);// 日志

    @Autowired
    private SeDoubtMapper seDoubtMapper;
    @Autowired
    private UtilsMapper utilsMapper;

    @Autowired
    private ZzuulFeign zzuulFeign;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public JsonResult addSeDoubt(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            try {
                if (params.get("HouId") == null || params.get("HouInfoId") == null ||
                        params.get("Remark") == null || params.get("Correct") == null) {
                    result.putFailed("参数不正确");
                }
                //疑问编号
                String UUID = "";
                Map<String, Object> uuid = utilsMapper.getUUID(new HashMap<String, Object>() {{
                    put("uuid", "DouId");
                    put("database", Constants.DATA_SOURCE_GMIO);
                    put("surface", "tcgmio_doubt");
                }});
                if (uuid != null) {
                    int number = Integer.valueOf(uuid.get("uuid").toString());
                    UUID = ComUtils.getLocalTrmSeqNum("do", number);
                } else {
                    UUID = ComUtils.getLocalTrmSeqNum("do", 0);
                }
                params.put("DouId", UUID);//房源疑问编号
                params.put("by", ContextUtils.getUserId());//操作人
                params.put("AppId", ContextUtils.getUserId());// 发起人编号
                params.put("term", "2");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(params);
                params.put("CreateTime", params.get("instm"));//疑问创建时间
                Integer count = seDoubtMapper.addSeDoubt(params);
                if (count > 0) {
                    result.putSuccess("操作成功");

                    //通知
                    CustomMap data = CustomMap.create("MeType", 13)
                            .put("Title", "房源疑问")
                            .put("Content", HtmlUtil.stringToHTML("注意！有新的房源疑问待处理 请及时处理！", "", Color.blue))
                            .put("by", ContextUtils.getUserId())
                            .put("IfJump", 1)
                            .put("Neturl", "/sys-QuestionCenter/questionHouse")
                            .put("term", 3);
                    zzuulFeign.AddMessage(data);

                } else {
                    result.putFailed("服务器繁忙");
                }

            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } finally {
            lock.unlock();
        }
        return result;
    }

}
